// 封装关于组织架构的api
import request from '@/utils/request'
/**
 *获取部门列表
 * @returns Promise
 */
export const getDepartmentList = () => {
  return request({
    method: 'get',
    url: '/company/department'
  })
}
/**
 * 部门列表数据转换树状结构
 * @param {} list Array
 * @param {*} pid 部门pid
 * @returns
 */
export const listToTree = (list, pid = '') => {
  const newList = []
  list.forEach(item => {
    if (item.pid === pid) {
      const result = listToTree(list, item.id)
      if (result.length > 0) {
        item.children = result
      }
      newList.push(item)
    }
  })
  return newList
}
/**
 * 删除部门
 * @param {*} id 部门id
 * @returns
 */
export const delDepartment = (id) => {
  return request({
    method: 'delete',
    url: '/company/department/' + id
  })
}
/**
 * 新增部门
 * @param {*} data 新增部门表单对象
 * @returns
 */
export const addDepartment = (data) => {
  return request({
    method: 'post',
    url: '/company/department',
    data
  })
}
/**
 * 获取部门详情
 * @param {} id 部门id
 * @returns
 */
export const getDepartmentDetail = (id) => {
  return request({
    method: 'get',
    url: '/company/department/' + id
  })
}
/**
 * 修改部门信息
 * @param {*} id 部门id
 * @returns
 */
export const eidtDepartmentDetail = (data) => {
  return request({
    method: 'put',
    url: '/company/department/' + data.id,
    data
  })
}
